package com.syezon.wifikey.download;

import android.os.Environment;
import android.text.TextUtils;

import com.hodanet.appadvplatclient.ad.AdInfo;
import com.syezon.android.base.download.DownloadManager;
import com.syezon.android.base.download.DownloadStatus;
import com.syezon.android.base.download.DownloadTask;
import com.syezon.android.base.download.IDownloadManager;
import com.syezon.android.base.utils.ApkUtil;
import com.syezon.android.base.utils.ToastUtils;
import com.syezon.wifikey.ad.AdPositionEnum;
import com.syezon.wifikey.utils.MD5;
import com.syezon.wifikey.utils.Utils;

import java.io.File;

/**
 * Created by Administrator on 2018/9/5.
 */

public class DownloadHelper {

    public static final String APK_SAVE_PATH = File.separator + "wifikey" + File.separator + "apk" + File.separator;

    /**
     * @param url
     * @return 获取文件的下载路径
     */
    public static String getApkSavePath(String url) {
        String urlId = new StringBuilder(url).append("com.syezon.wifi").toString();
        String appUrlId = MD5.md5(urlId);
        String finalFileName = appUrlId + ".apk";
        String path = Environment.getExternalStorageDirectory().getPath() + APK_SAVE_PATH + finalFileName;
        return path;
    }

    public static DownloadTask getDownloadTask(String url) {
        if (TextUtils.isEmpty(url)) {
            return null;
        }
        String urlId = new StringBuilder(url).append("com.syezon.wifi").toString();
        String appUrlId = MD5.md5(urlId);
        String finalFileName = appUrlId + ".apk";
        String path = Environment.getExternalStorageDirectory().getPath() + APK_SAVE_PATH;
        DownloadTask task = new DownloadTask();
        task.setFileName(finalFileName);
        task.setSaveDirPath(path);
        task.setId(appUrlId);
        task.setUrl(url);
        return task;
    }

    /**
     * @param url
     * @return 判断apk链接是否下载到本地
     */
    public static boolean checkAppDownload(String url) {
        if (TextUtils.isEmpty(url)) {
            return false;
        }
        File file = new File(getApkSavePath(url));
        return file.exists();
    }

    public static DownloadTask downloadUrl(String url) {
        DownloadTask downloadTask = null;
        try {
            if (!TextUtils.isEmpty(url)) {
                if (checkAppDownload(url)) {
                    ApkUtil.ApkInfo apkInfo = ApkUtil.getApkInfo(Utils.getApp(), getApkSavePath(url));
                    if (apkInfo != null) {
                        if (ApkUtil.checkInstall(Utils.getApp(), apkInfo.pkgName)) {
                            ApkUtil.openApp(Utils.getApp(), apkInfo.pkgName);
                        } else {
                            ApkUtil.installApk(Utils.getApp(), getApkSavePath(url));
                        }
                    }
                } else {
                    DownloadTask task = getDownloadTask(url);
                    if (task != null) {
                        IDownloadManager downloadMgr = DownloadManager.getInstance(Utils.getApp());
                        ToastUtils.showShort("正在下载...");
                        downloadTask = downloadMgr.addDownloadTask(task, new UrlDownloadListener(url));
                        if (downloadTask != null) {
                            if (downloadTask.getDownloadStatus() != DownloadStatus.DOWNLOAD_STATUS_DOWNLOADING) {
                                downloadMgr.resume(downloadTask.getId());
                            }
                        } else {
                            downloadTask = task;
                        }
                    }
                }
            }
        } catch (Exception e) {
        }
        return downloadTask;
    }

    public static DownloadTask downloadUrl(AdInfo adInfo, AdPositionEnum adPositionEnum) {
        DownloadTask downloadTask = null;
        try {
            if (!TextUtils.isEmpty(adInfo.getUrl())) {
                if (checkAppDownload(adInfo.getUrl())) {
                    ApkUtil.ApkInfo apkInfo = ApkUtil.getApkInfo(Utils.getApp(), getApkSavePath(adInfo.getUrl()));
                    if (apkInfo != null) {
                        if (ApkUtil.checkInstall(Utils.getApp(), apkInfo.pkgName)) {
                            ApkUtil.openApp(Utils.getApp(), apkInfo.pkgName);
                        } else {
                            ApkUtil.installApk(Utils.getApp(), getApkSavePath(adInfo.getUrl()));
                        }
                    }
                } else {
                    DownloadTask task = getDownloadTask(adInfo.getUrl());
                    if (task != null) {
                        IDownloadManager downloadMgr = DownloadManager.getInstance(Utils.getApp());
                        ToastUtils.showShort("正在下载...");
                        downloadTask = downloadMgr.addDownloadTask(task, new HodaAdDownloadListener(adInfo, adPositionEnum));
                        if (downloadTask != null) {
                            if (downloadTask.getDownloadStatus() != DownloadStatus.DOWNLOAD_STATUS_DOWNLOADING) {
                                downloadMgr.resume(downloadTask.getId());
                            }
                        } else {
                            downloadTask = task;
                        }
                    }
                }
            }
        } catch (Exception e) {
        }
        return downloadTask;
    }
}
